package com.demo.utils.exception;

import com.demo.utils.base.WebResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理器
 *
 * @author ruoyi
 */
@RestControllerAdvice
public class GlobalExceptionHandler
{
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     * 业务异常
     */
    @ExceptionHandler(BizServiceException.class)
    public WebResponse handleServiceException(BizServiceException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',业务异常'{}'", requestURI, e.getMessage());
        Integer code = e.getCode();
        return code != null ? WebResponse.error(e) : WebResponse.error(e.getMessage());
    }

}
